﻿using System;
using Microsoft.Office.Interop.Excel;
using TrafficCountingTool.Models;

namespace TrafficCountingTool.Core
{
    public enum ExportingFileType
    {
        Txt,
        Xlsx,
        Xls
    }

    public static class ExportingFileHelper
    {
        public static void Export(AppCache appCache, ExportingFileType type, string path)
        {
            switch (type)
            {
                case ExportingFileType.Txt:
                    ExportToTxt(appCache, path);
                    break;
                case ExportingFileType.Xlsx:
                    ExportToXlsx(appCache, path);
                    break;
            }
        }

        private static void ExportToXlsx(AppCache appCache, string path)
        {
            //Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();
            Application excel = new ApplicationClass();

            Workbook workbook = excel.Application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet worksheet = (Worksheet)workbook.Worksheets[1];

            for (int i = 0; i < appCache.VehicleCountings.Count; i++)
            {
                worksheet.Cells[1, i + 1] = appCache.VehicleCountings[i].VehicleType;
                worksheet.Cells[2, i + 1] = appCache.VehicleCountings[i].Count;
            }

            workbook.SaveAs(path);
            workbook.Close(true);
            excel.Quit();   
        }

        private static void ExportToTxt(AppCache appCache, string path)
        {
            
        }
    }
}